#include <glf.h>

#if defined(GLF_PLATFORM_WIN32)
#	include <Windows.h>
#elif defined(GLF_PLATFORM_LINUX)
#	include <sys/time.h>
#else
#	error Unsupported Platform
#endif

#if defined ( GLF_PLATFORM_WIN32 )
static LARGE_INTEGER g_frequency = { 0 };
static LARGE_INTEGER g_previousTime = { 0 };
#else
static timeval g_previousTime = 0;
#endif

void glfResetTime()
{
#if defined( GLF_PLATFORM_WIN32 )
	QueryPerformanceFrequency( &g_frequency );
	QueryPerformanceCounter(&g_previousTime);
#else
	gettimeofday(&g_previousTime, 0);
#endif
}

double glfTimeDelta()
{
#if defined( GLF_PLATFORM_WIN32 )
	LARGE_INTEGER time;
	unsigned long long elapsedTime;
	double d;
	QueryPerformanceCounter(&time);
	elapsedTime = time.QuadPart - g_previousTime.QuadPart;
	g_previousTime = time;
	d=(double)(elapsedTime)/(g_frequency.QuadPart);
	return d;
#else
	timeval time;
	gettimeofday(&time, 0);
	double elapsedTime = (time.tv_sec+time.tv_usec/1000000.0) - (g_previousTime.tv_sec+g_previousTime.tv_usec/1000000.0);
	g_previousTime = time;
	return elapsedTime;
#endif
}